<?php
namespace App\Http\Controllers\Admin;
use App\Models\Article;
use App\Models\ArticleType;
use App\Services\CommonService;
use Illuminate\Http\Request;

//平台文章列表
class PlatformArticleController extends BaseController
{
    //平台文章列表
    public function platformArticleList(Request $request)
    {
        $input = $request->input();
        $where = [];
        $where[] = ['a.is_delete', Article::ARTICLE_NOT_DELETE];
        $where[] = ['at.is_delete', ArticleType::ARTICLE_TYPE_NOT_DELETE];
        if (!empty($input['article_title'])) {
            $where[] = ['a.article_title', 'like', '%' . trim($input['article_title']) . '%'];
        }
        if (!empty($input['status'])) {
            $where[] = ['a.status', $input['status']];
        }
        if (!empty($input['parentid'])) {
            $where[] = ['a.parentid', $input['parentid']];
        }
        if (!empty($input['publish_time'])) {
            $time = searchTime($input['publish_time']);
            $where[] = ['a.publish_time', '>=', $time['begin_time']];
            $where[] = ['a.publish_time', '<', $time['end_time']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = Article::GetPlatformArticleList($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v){
            $parent_name = ArticleType::GetOneArticleType(array('id' => $v['parentid']));
            $info[$k]['type_name'] = $parent_name['type_name'].'->'.$v['type_name'];
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //获取二级分类
    public function secondCategory(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章分类id为空', array());
        }
        $data = ArticleType::GetSecondCategory(array('parentid' => $input['id']));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //添加或编辑平台文章
    public function addOrEditPlatformArticle(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) { //添加
            if (empty($input['article_title'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入文章标题');
            }
            if (empty($input['type_id'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择文章分类id');
            }
            if (empty($input['parentid'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择一级分类id');
            }
            if (empty($input['article_img'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请上传缩略图');
            }
            if (empty($input['abstract'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入文章摘要');
            }
            if (empty($input['content'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入内容');
            }
            if (empty($input['status'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择状态');
            }
            if (empty($input['home_recommend'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择首页推荐');
            }
            if (empty($input['hot_recommend'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择热门推荐');
            }
            if (empty($input['pc_show'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择PC商城');
            }
            if (empty($input['h5_show'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择H5商城');
            }
            if (empty($input['xcx_show'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择支付宝小程序');
            }
            if (empty($input['author'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入作者');
            }
            $new_admin = $this->users;
            $input['uid'] = $new_admin['id'];
            $input['update_time'] = date('Y-m-d H:i:s');
            $input['publish_time'] = date('Y-m-d H:i:s');
            $input['is_delete'] = Article::ARTICLE_NOT_DELETE;
            $res = Article::addData($input);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
            }
        } else { //编辑
            $input['update_time'] = date('Y-m-d H:i:s');
            Article::editDataById($input['id'], $input);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
        }

    }

    //根据id查询平台文章 http://shop.cn/Admin/PlatformArticle/platformArticleById?id=
    public function platformArticleById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章信息id为空', array());
        }
        $data = Article::GetArticleById($input['id']);
        if (empty($data)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章分类数据为空', array());
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //根据id编辑首页推荐POST http://shop.cn/Admin/PlatformArticle/editHomeRecommend  id=&home_recommend=
    public function editHomeRecommend(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章信息id为空', array());
        }
        if (empty($input['home_recommend'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '首页推荐为空', array());
        }
        Article::editDataById($input['id'], array('home_recommend' => $input['home_recommend'],'update_time' => date('Y-m-d H:i:s')));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
    }

    //根据id编辑热门推荐POST http://shop.cn/Admin/PlatformArticle/editHotRecommend  id=&hot_recommend=
    public function editHotRecommend(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章信息id为空', array());
        }
        if (empty($input['hot_recommend'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '热门推荐为空', array());
        }
        Article::editDataById($input['id'], array('hot_recommend' => $input['hot_recommend'],'update_time' => date('Y-m-d H:i:s')));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
    }

    //根据id编辑文章信息状态POST http://shop.cn/Admin/PlatformArticle/editArticleStatus  id=&status=
    public function editArticleStatus(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章信息id为空', array());
        }
        if (empty($input['status'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '状态为空', array());
        }
        Article::editDataById($input['id'], array('status' => $input['status'],'update_time' => date('Y-m-d H:i:s')));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
    }

    //根据id删除平台文章POST http://shop.cn/Admin/PlatformArticle/delPlatformArticle?id=
    public function delPlatformArticle(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '文章信息id为空');
        }
        Article::editDataById($input['id'], array('is_delete' => Article::ARTICLE_IS_DELETE,'update_time' => date('Y-m-d H:i:s')));
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '删除成功');
    }


}
